package com.zzpct.wh.work.whactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.zzpct.wh.work.whactivity.entity.CarsInfo;
import com.zzpct.wh.work.whactivity.entity.QicheguzhangInfo;
import com.zzpct.wh.work.whactivity.service.CarlistService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    TextView textView;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.tv);
        getCarInfo();
        getQicheguzhang();
        initView();
    }

    private void initView() {
        lv=findViewById(R.id.lv_jhkh);

    }

    private void getQicheguzhang() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/obdcode/query")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        CarlistService carlistService=retrofit.create(CarlistService.class);
        Call<QicheguzhangInfo> call=carlistService.getQicheguzhangAll("98b4ce365f40f032dbaf90a89d22c3b2",
                "P2079");
        call.enqueue(new Callback<QicheguzhangInfo>() {
            @Override
            public void onResponse(Call<QicheguzhangInfo> call, Response<QicheguzhangInfo> response) {
                QicheguzhangInfo qi=response.body();
                QicheguzhangInfo.ResultBean resultDTO=qi.getResult();
                resultDTO.save();
                Log.d(TAG, "onResponse: "+resultDTO.getZwhy());
            }

            @Override
            public void onFailure(Call<QicheguzhangInfo> call, Throwable t) {

            }
        });

    }

    private void getCarInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CarlistService carlistService=retrofit.create(CarlistService.class);
        Call<CarsInfo> all =carlistService.getAll();
        all.enqueue(new Callback<CarsInfo>() {
            @Override
            public void onResponse(Call<CarsInfo> call, Response<CarsInfo> response) {
                CarsInfo ca =response.body();
                String s= ca.getData().get(0).getCarplate();
                textView.setText(s);
            }

            @Override
            public void onFailure(Call<CarsInfo> call, Throwable t) {

            }
        });
    }
}
